package per.zqx.workQueue;

import com.rabbitmq.client.*;
import per.zqx.utils.RabbitUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Consumer_unfair_01 ，公平分发消费者
 *
 * @author Sa  2021-03-17 10:27
 */
public class Consumer_fair_01 {
    public static void main(String[] args) throws IOException, TimeoutException {
        final String QUEUE_NAME = "work_queue_unfair";
        Channel channel = RabbitUtils.getConnection().createChannel();
        // 告知一个消费者同时只能够消费一个任务
        channel.basicQos(1);
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        DeliverCallback deliverCallback = ((consumerTag, message) -> {
            String msg = new String(message.getBody(), "utf-8");
            System.out.println("Consumer_unfair_01 received a message : " + msg);
            // 手动确认
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        });
        // 必须关闭自动确认，进行手动确认
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME, autoAck, deliverCallback, consumerTag -> {
        });
    }
}
